//bind.js
var utilMd5 = require('../../../../../utils/md5.js');
var utilCommon = require('../../../../../utils/common.js');
var common = require("../../../../../utils/common.js");
//获取应用实例
var app = getApp()
Page({
  data: {
    btnBinding:false,
    bindText:"绑定",
    account:"",
    password:"",
    formId:""
  },
  onLoad: function () {
    
  },
  btnBind: function (e) {
    console.log(e.detail.formId)
    var that = this
    // 设置按钮加载状态
    this.setData({
      btnBinding:true,
      bindText:"正在保存",
      formId:e.detail.formId
    })
    this.submitData(function(res){
        //更新按钮状态
        that.setData({
          btnBinding:false,
          bindText:"完成"
        })
      if(res.status==-1){
        
      }else{
        // 存储userInfo.binduserinfo
        app.globalData.userInfo.binduserinfo=res.data
        //刷新上一页数据
        var pages = getCurrentPages();
        if(pages.length > 2){
            //上上一个页面实例对象
            var preperPage = pages[1];
            preperPage.getData()
            //上一个页面实例对象
            var prePage = pages[2];
            prePage.bindCallback()
        }
        wx.navigateBack({
          delta: pages.length - 2
        })
      }
    })
  },
  submitData: function(cb) {
    var that = this;
    var account = that.data.account
    var password = that.data.password

    var returnRes = null
    if(account=="") {
      wx.showModal({
        title: '错误提示',
        content: '账号不能为空',
        showCancel:false
      })
      returnRes = {
      "status":-1,
      "msg":"账号不能为空"
      }
      typeof cb == "function" && cb(returnRes)
      return
    }
    if(password=="") {
      wx.showModal({
        title: '错误提示',
        content: '密码不能为空',
        showCancel:false
      })
      returnRes = {
      "status":-1,
      "msg":"密码不能为空"
      }
      typeof cb == "function" && cb(returnRes)
      return
    }
    var newPwd = this.newPassword(account,password)
    console.log(app.globalData.wangcaiToken)
    console.log(account)
    console.log(newPwd)
    console.log(that.data.formId)
    wx.request({
      url: common.apiUrl()+'bindAccount',
      data: {
        token:app.globalData.wangcaiToken,
        account:account,
        password:newPwd,
        formid:that.data.formId
      },
      method:'POST',
      header: {
      'content-type': 'application/x-www-form-urlencoded'
        },
      success: function(res){
        if(res.data.status==1){
          wx.showToast({
            title: '绑定成功',
            icon: 'success',
            success: function(e){
              typeof cb == "function" && cb(res.data)
            }
          })
        } else {
          console.log(res.data)
          app.errorToGetToken()
        }
      }
    })
  },
  accountInput: function(e) {
    this.setData({
      account: e.detail.value
    })
  },
  passwordInput: function(e) {
    this.setData({
      password: e.detail.value
    })
  },
  newPassword: function(a,b) {
    var newPwd = utilCommon.funEncryption(a,b);
    return utilMd5.md5(newPwd);
  }
})
